
package com.admissions.tracnghiem;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.admissions.AdmissionsActivity;
import com.admissions.R;
import com.admissions.TabGroupActivity;
import com.admissions.adapter.MonThiAdapter;
import com.admissions.model.MonThi;

public class ThiTracNghiem extends Activity implements OnItemClickListener {
    private Resources mRes;

    private GridView mGrid;

    private boolean mNeededLoad = true;

    private ArrayList<MonThi> mMonThis = new ArrayList<MonThi>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thi_trac_nghiem);
        mRes = getResources();
        init();

        mGrid = (GridView) findViewById(R.id.grid_items);
        mGrid.setNumColumns(2);
        mGrid.setOnItemClickListener(this);
        mGrid.setAdapter(new MonThiAdapter(this, mMonThis));
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mNeededLoad) {

        }

        // Setup Header
        String headerContent = mRes.getString(R.string.header_luyen_thi_trac_nghiem);
        String headerComment = mRes.getString(R.string.header_comment_luyen_thi_trac_nghiem);
        Drawable icon = mRes.getDrawable(R.drawable.icon);
        TabGroupActivity parent = (TabGroupActivity) getParent();
        AdmissionsActivity grandParent = (AdmissionsActivity) parent.getParent();
        grandParent.setupHeader(icon, headerContent, headerComment);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        MonThi monthi = (MonThi) parent.getItemAtPosition(position);
        Intent intent = new Intent(getParent(), SectionActivity.class);

        Bundle bS = new Bundle();
        bS.putSerializable(MonThi.MONTHI, monthi);
        intent.putExtras(bS);

        TabGroupActivity parentActivity = (TabGroupActivity) getParent();
        parentActivity.startChildActivity(SectionActivity.class.getName(), intent);
    }

    public void init() {
        String description = mRes.getString(R.string.thi_thu);

        // Ten cac mon thi
        String mon_tieng_anh = mRes.getString(R.string.mon_tieng_anh);
        String mon_tieng_phap = mRes.getString(R.string.mon_tieng_phap);
        String mon_tieng_duc = mRes.getString(R.string.mon_tieng_duc);
        String mon_tieng_trung = mRes.getString(R.string.mon_tieng_trung);
        String mon_tieng_nhat = mRes.getString(R.string.mon_tieng_nhat);
        String mon_vat_ly = mRes.getString(R.string.mon_vat_ly);
        String mon_hoa_hoc = mRes.getString(R.string.mon_hoa_hoc);
        String mon_sinh_hoc = mRes.getString(R.string.mon_sinh_hoc);

        // Icon cac mon thi
        Bitmap ic_mon_tieng_anh = BitmapFactory.decodeResource(mRes, R.drawable.ic_mon_tieng_anh);
        Bitmap ic_mon_tieng_phap = BitmapFactory.decodeResource(mRes, R.drawable.ic_mon_tieng_phap);
        Bitmap ic_mon_tieng_duc = BitmapFactory.decodeResource(mRes, R.drawable.ic_mon_tieng_duc);
        Bitmap ic_mon_tieng_trung = BitmapFactory.decodeResource(mRes,
                R.drawable.ic_mon_tieng_trung);
        Bitmap ic_mon_tieng_nhat = BitmapFactory.decodeResource(mRes, R.drawable.ic_mon_tieng_nhat);
        Bitmap ic_mon_vat_ly = BitmapFactory.decodeResource(mRes, R.drawable.ic_mon_vat_ly);
        Bitmap ic_mon_hoa_hoc = BitmapFactory.decodeResource(mRes, R.drawable.ic_mon_hoa_hoc);
        Bitmap ic_mon_sinh_hoc = BitmapFactory.decodeResource(mRes, R.drawable.ic_mon_sinh_hoc);

        // Khoi tao cac mon thi
        MonThi monTiengAnh = new MonThi(ic_mon_tieng_anh, MonThi.TIENG_ANH, mon_tieng_anh,
                description);
        MonThi monTiengPhap = new MonThi(ic_mon_tieng_phap, MonThi.TIENG_PHAP, mon_tieng_phap,
                description);
        MonThi monTiengDuc = new MonThi(ic_mon_tieng_duc, MonThi.TIENG_DUC, mon_tieng_duc,
                description);
        MonThi monTiengTrung = new MonThi(ic_mon_tieng_trung, MonThi.TIENG_TRUNG, mon_tieng_trung,
                description);
        MonThi monTiengNhat = new MonThi(ic_mon_tieng_nhat, MonThi.TIENG_NHAT, mon_tieng_nhat,
                description);
        MonThi monVatLy = new MonThi(ic_mon_vat_ly, MonThi.VAT_LY, mon_vat_ly, description);
        MonThi monHoaHoc = new MonThi(ic_mon_hoa_hoc, MonThi.HOA_HOC, mon_hoa_hoc, description);
        MonThi monSinhHoc = new MonThi(ic_mon_sinh_hoc, MonThi.SINH_HOC, mon_sinh_hoc, description);

        // Add toi ArrayList
        mMonThis.add(monTiengAnh);
        mMonThis.add(monTiengPhap);
        mMonThis.add(monTiengDuc);
        mMonThis.add(monTiengTrung);
        mMonThis.add(monTiengNhat);
        mMonThis.add(monVatLy);
        mMonThis.add(monHoaHoc);
        mMonThis.add(monSinhHoc);
    }
}
